package com.linsen.core.mybatisplus.handler;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.linsen.core.common.context.TokenContextHolder;
import com.linsen.core.common.enums.DeleteType;
import com.linsen.core.common.model.User;
import com.linsen.core.mybatisplus.constant.EntityConstant;
import com.linsen.core.mybatisplus.props.LinsenMyBatisPlusProperties;
import com.linsen.core.redis.util.RedisSecurityUtil;
import com.linsen.uaa.provider.feign.IAuthFeignClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * 自动填充功能
 *
 * @author zhaoyong
 * @date 2023/3/11
 */
@Slf4j
@RequiredArgsConstructor
public class LinsenMetaObjectHandler implements MetaObjectHandler {
    private final IAuthFeignClient authFeignClient;
    private final RedisSecurityUtil redisSecurityUtil;
    private final LinsenMyBatisPlusProperties myBatisPlusProperties;

    @Override
    public void insertFill(MetaObject metaObject) {
        if (BooleanUtil.isFalse(this.myBatisPlusProperties.getMetaObjectHandler())) {
            return;
        }

        this.strictInsertFill(metaObject, EntityConstant.CREATE_TIME_PROPERTY, LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, EntityConstant.UPDATE_TIME_PROPERTY, LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, EntityConstant.DELETED_PROPERTY, DeleteType.NORMAL::getValue, Integer.class);

//        Result result = this.authFeignClient.getUser();
//        User user = result.getData(User.class);
        try {
            User user = this.redisSecurityUtil.getUser(TokenContextHolder.getToken());

            String userId = user.getUserid();
            String username = user.getUsername();
            String deptId = user.getDeptId();
            String tenantId = user.getTenantId();
            this.strictInsertFill(metaObject, EntityConstant.CREATE_BY_PROPERTY, () -> userId, String.class);
            this.strictInsertFill(metaObject, EntityConstant.CREATE_NAME_BY_PROPERTY, () -> username, String.class);
            this.strictInsertFill(metaObject, EntityConstant.UPDATE_BY_PROPERTY, () -> userId, String.class);
            this.strictInsertFill(metaObject, EntityConstant.UPDATE_NAME_BY_PROPERTY, () -> username, String.class);
            this.strictInsertFill(metaObject, EntityConstant.TENANT_ID_PROPERTY, () -> tenantId, String.class);
        }catch (Exception e){

        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (BooleanUtil.isFalse(this.myBatisPlusProperties.getMetaObjectHandler())) {
            return;
        }

        this.strictInsertFill(metaObject, EntityConstant.UPDATE_TIME_PROPERTY, LocalDateTime::now, LocalDateTime.class);

//        Result result = this.authFeignClient.getUser();
//        User user = result.getData(User.class);
        try {
            User user = this.redisSecurityUtil.getUser(TokenContextHolder.getToken());

            String userId = user.getUserid();
            String username = user.getUsername();
            this.strictInsertFill(metaObject, EntityConstant.UPDATE_BY_PROPERTY, () -> userId, String.class);
            this.strictInsertFill(metaObject, EntityConstant.UPDATE_NAME_BY_PROPERTY, () -> username, String.class);
        }catch (Exception e){

        }
    }
}
